 
  

 






<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do?Id=166 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:28:07 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
<head>
 <title>
  Java Practices -> Layout Managers
 </title>
 <link rel="stylesheet" type="text/css" href="../stylesheet8.css" media="all">
 
 <link rel="shortcut icon" href='../images/favicon.ico' type="image/vnd.microsoft.icon">
 <meta name="description" content="Concise presentations of java programming practices, tasks, and conventions, amply illustrated with syntax highlighted code examples.">
 
 <meta name='keywords' content='BorderLayout,Box,BoxLayout,GridBagLayout,layout,swing,java,java programming,java practices,java idiom,java style,java design patterns,java coding conventions,'>
 
 
</head>
 
<body>


<div class='menu-bar'>
 
  <a href='../home/HomeAction.html' title='Table of Contents'>Home</a> |
  <a href='../vote/VoteSummaryAction-2.html' title='View Poll Results'>Poll</a> |
   
  <A href='../feedback/FeedbackAction451f-2.html?Operation=Show' title='Send Your Feedback'>Wiki</a> |
  <b><a href='../source/SourceAction-2.html' title='Grab Source Code'>Source Code</a></b><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |

  <a href='http://www.web4j.com/Java_Web_Application_Framework_Overview.jsp?From=1' title='Free Download - Java Web Application Framework'><b>WEB4J</b></a> |
  
  <a href='http://www.date4j.net/' title='Replacement for java.util.Date'><b>DATE4J</b></a> |

   <a href='../references/ReferencesAction-2.html' title='References'>Links</a>
   
  <form action='http://www.javapractices.com/search/SearchAction.do' method='get' class='search-form'>
   <input type='text' name='SearchTerms' value="" size=12 maxlength=50 class='search'>
   <input type='submit' value="Search">
  </form>
 
</div>

<P>



  

 






<p class="display-messages">

 

 

</p>


<div class="main-layout">
 
   

 




<div class='page-title'>Layout Managers</div>

<div class='main-body'>
 
<br>Layout Managers should almost always be used. Hard coding positions
and sizes is usually not appropriate.
<p>Most common layouts :
<ul>
<li>
<tt><a href="http://java.sun.com/javase/6/docs/api/java/awt/GridBagLayout.html">GridBagLayout</a></tt>
- excellent when many components involved</li>

<li>
<tt><a href="http://java.sun.com/javase/6/docs/api/javax/swing/BoxLayout.html">BoxLayout</a></tt>
(and associated <tt>Box</tt> class) - "glue" and "struts" (defined in <tt><a href="http://java.sun.com/javase/6/docs/api/javax/swing/Box.html">Box</a></tt>,
not <tt>BoxLayout</tt>), combined with proper alignment and equalization
of sizes, produce results pleasing to the eye</li>

<li>
<tt><a href="http://java.sun.com/javase/6/docs/api/java/awt/BorderLayout.html">BorderLayout</a></tt>
- often suitable for a top level container</li>
</ul>
Least common layouts :
<ul>
<li>
<tt><a href="http://java.sun.com/javase/6/docs/api/java/awt/FlowLayout.html">FlowLayout</a></tt>
- items can appear on different rows, according to size of container</li>

<li>
<tt><a href="http://java.sun.com/javase/6/docs/api/java/awt/GridLayout.html">GridLayout</a></tt>
- forces all components to be the same size</li>

<li>
<tt><a href="http://java.sun.com/javase/6/docs/api/javax/swing/SpringLayout.html">SpringLayout</a></tt>
- new to JDK 1.4</li>
</ul>
Layout Managers are often mixed together in a single frame or dialog, where
a top level container has its own Layout Manager (often a <tt>BoxLayout</tt>
or <tt>BorderLayout</tt>), and smaller parts use their own layout, completely
independent of the others.

<P>Please see the topic on <a href="TopicAction08d2-2.html?Id=236">JGoodies Forms</a> for an interesting alternative to the standard JDK classes for layouts. 

<p><b>Example 1</b>
<p>Here is an example of a dialog box which uses four nested <tt>BoxLayout</tt>
objects to arrange its elements. The brightly colored areas group elements
which share the same <tt>BoxLayout</tt> :
<p><img SRC="../images/EditPortfolioShowingBoxes.gif" ALT="Highlighted layout managers" BORDER=0 height=242 width=613>
<p><b>Red</b> - (vertical layout) "struts" provide uniform spacing around
each <tt>JButton</tt>, while "glue" takes up the remaining extra space
below the <tt>Delete</tt> button
<p><b>Blue</b> - (horizontal layout) again, struts provide uniform spacing
around each <tt>JButton</tt>, and glue takes up the remaining extra space
to the left of the <tt>OK</tt> button.
<p><b>Black</b> - (horizontal layout) contains the <tt>JTable</tt> and
the <tt>Add-Change-Delete</tt> buttons
<p><b>Yellow</b> - (vertical layout) the top level container for Black
and Blue
<p>Yellow and Blue form a <a href="TopicAction7910-2.html">standardized dialog</a>,
in which top level layout and the <tt>OK</tt>, <tt>Cancel</tt> buttons
are defined by an abstract base class. Concrete subclasses define both
the contents of Black and the action taken by the <tt>OK</tt> button.
<p>The sizing and alignment of <tt>JButton</tt> objects in <tt>BoxLayout</tt>
is not automatic, unfortunately. See <a href="TopicAction2994-2.html">Common utility
tasks</a> for these related utility methods :
<ul>
<li>
<tt>getCommandRow</tt></li>

<li>
<tt>getCommandColumn</tt></li>

<li>
<tt>equalizeSizes</tt></li>

<li>
<tt>alignAllX</tt>, <tt>alignAllY</tt></li>
</ul>
<b>Example 2</b>
<p>Here is a typical data entry dialog :
<p><img SRC="../images/ChangeStockShowingBoxes.gif" ALT="Highlighted layout managers" BORDER=0 height=196 width=352>
<p><b>Blue</b> - the standardized command row (same as Example 1)
<p><b>Black</b> - uses <tt>GridBagLayout</tt>.&nbsp; See <a href="TopicAction2994-2.html">Common
utility tasks</a> for these utility methods related to <tt>GridBagLayout</tt>
and <tt>GridBagConstraints</tt> :
<ul>
<li>
<tt>addSimpleEntryField</tt> - add a related <tt>JLabel</tt> and <tt>JTextField</tt>
to a <tt>GridBagLayout</tt></li>

<li>
<tt>addSimpleDisplayField</tt> - add a pair of related <tt>JLabel</tt>
objects to a <tt>GridBagLayout</tt></li>

<li>
<tt>getConstraints</tt> - a default set of constraints, which can be modified
as desired</li>
</ul>
<b>Yellow</b> - the standardized top level layout (same as Example 1)
<br>
<br>

</div>




<div class='topic-section'>See Also :</div>
<div class='main-body'>
 
  
  <a href='TopicAction7910-2.html?Id=150'>Standardized dialogs</a> <br>
 
  
  <a href='TopicAction2994-2.html?Id=152'>Swing utility tasks</a> <br>
 
  
  <a href='TopicAction08d2-2.html?Id=236'>Consider JGoodies Forms for layouts</a> <br>
 
</div>


<div class='topic-section'>Would you use this technique?</div>
<div class='main-body'>
  
  <form action="http://www.javapractices.com/vote/AddVoteAction.do" method='post'>
    Yes<input type='radio' name='Choice' value='Y' >
    &nbsp;&nbsp;No<input type='radio' name='Choice' value='N'>
    &nbsp;&nbsp;Undecided<input type='radio' name='Choice' value="?" >
    &nbsp;&nbsp;<input type=submit value="Vote" >
    <input type='hidden' name='Operation' value='Apply'>
    <input type='hidden' name='TopicId' value='166'>
  </form>
</div>

<div style='height:10.0em;'></div>

 
 
</div>

  

 





<div align='center' class='legalese'>  
&copy; 2011 Hirondelle Systems |
<a href='../source/SourceAction-2.html'><b>Source Code</b></a><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |
<a href="mailto:webmaster@javapractices.com">Contact</a> |
<a href="http://creativecommons.org/licenses/by-nc-sa/1.0/">License</a> |
<a href='../apps/cjp.rss'>RSS</a>
<!-- ukey="2AC36CD2" -->
<!-- ckey="16DF3D87" -->
<br>

 Individual code snippets can be used under this <a href='../LICENSE.txt'>BSD license</a> - Last updated on June 6, 2010.<br>
 Over 150,000 unique IPs last month - <span title='Java Practices 2.6.5, Mon May 16 00:00:00 EDT 2011'>Built with</span> <a href='http://www.web4j.com/'>WEB4J</a>.<br>
 - In Memoriam : Bill Dirani -
</div>

<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2633428-1";
urchinTracker();
</script>



</body>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do?Id=166 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:28:07 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
</html>
